package com.dlws.modules.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.dlws.common.utils.Constants;
import com.dlws.common.utils.DateUtils;
import com.dlws.common.web.BaseController;
import com.dlws.modules.field.entity.FieldPrice;
import com.dlws.modules.field.entity.ProductField;
import com.dlws.modules.field.entity.ProductFieldValue;
import com.dlws.modules.field.service.FieldPriceService;
import com.dlws.modules.field.service.ProductFieldService;
import com.dlws.modules.field.service.ProductFieldValueService;
import com.dlws.modules.order.entity.Order;
import com.dlws.modules.order.entity.OrderFieldPriceVo;
import com.dlws.modules.order.entity.OrderFieldVo;
import com.dlws.modules.order.entity.OrderPageVo;
import com.dlws.modules.order.entity.OrderPassFieldVo;
import com.dlws.modules.order.entity.OrderPassVo;
import com.dlws.modules.order.entity.OrderProductCustomer;
import com.dlws.modules.order.entity.OrderProductWeb;
import com.dlws.modules.order.service.OrderProductCustomerService;
import com.dlws.modules.order.service.OrderProductWebService;
import com.dlws.modules.order.service.OrderService;
import com.page.Pagination;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderProductWebService opwService;

	@Autowired
	private OrderProductCustomerService opcService;
	
	@Autowired
	private ProductFieldService pfService;
	
	@Autowired
	private ProductFieldValueService pfvService;
	
	@Autowired
	private FieldPriceService fpService;

	/**
	 * 
	 * 描述：订单列表
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-1 下午3:01:35
	 * @version
	 */
	@RequestMapping("/orderList")
	public String orderList(ModelMap model, Integer pageNo, HttpServletRequest request) {
		logger.info("--进入订单列表方法--");
		// List<Order> queryAll = orderService.queryAll(null);
		// if(null!=queryAll && queryAll.size()>0){
		// model.put("orderList", queryAll);
		// }
		StringBuilder params = new StringBuilder();
		OrderPageVo order = new OrderPageVo();
		// 如果页号为null 或小于1 设为1
		order.setPageNo(Pagination.cpn(pageNo));
		order.setPageSize(Constants.PAGE_SIZE);
		order.setIsDelete(0);
		//order.setOrderStatus(0);

		Pagination pagination = orderService.pagelist(order);
		// 分页展示
		String url = request.getContextPath() + "/order/orderList.do";
		pagination.pageView(url, params.toString());
		model.put("page", pagination);
		return "/order/orderlist";
	}
	
	
	/**
	 * 
	 * 描述：修改客户信息
	 * 创建人： 魏泽超
	 * 创建时间: 2016-7-22 上午9:41:15
	 * @version
	 */
	@ResponseBody
	@RequestMapping("/updateUserInfo.do")
	public Map<String,Object> updateUserInfo(Order order){
		Map<String,Object> map=new HashMap<String,Object>();
		int i = orderService.updateByPrimaryKeySelective(order);
		if(i>0){
			map.put("status", true);
		}else{
			map.put("status", false);
		}
		return map;
	}

	/**
	 * 
	 * 描述：不可操作产品详情--
	 * 历史产品记录--可供查询
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-1 下午4:02:32
	 * @version
	 */
	@RequestMapping("/detaliList")
	public String orderDetaliList(String orderNo, ModelMap model) {
		logger.info("--进入订单详情方法--订单编号为：" + orderNo);
		OrderProductWeb orderProductWeb = new OrderProductWeb();
		orderProductWeb.setOrderNo(orderNo);
		List<OrderProductWeb> queryAll = opwService.queryAll(orderProductWeb);
		if (null != queryAll && queryAll.size() > 0) {
			model.put("detailList", queryAll);
		}
		return "/orderdetail/list";
	}

	/**
	 * 
	 * 描述：可操作产品详情
	 * 修改价格等操作
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-14 上午10:14:16
	 * @version
	 */
	@RequestMapping("/detaliListCustomer")
	public String orderDetaliListCustomer(String orderNo, ModelMap model) {
		logger.info("--进入订单详情方法--订单编号为：" + orderNo);
		OrderProductCustomer opc = new OrderProductCustomer();
		opc.setOrderNo(orderNo);
		List<OrderProductCustomer> queryAll = opcService.queryAll(opc);
		if (null != queryAll && queryAll.size() > 0) {
			model.put("detailList", queryAll);
		}
		return "/orderdetail/list";
	}

	/**
	 * 
	 * 描述：订单审核
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-14 上午10:35:02
	 * @version
	 */
	@RequestMapping("/orderPass")
	public String orderPass(ModelMap model, String orderNo) {
		logger.info("--进入产品页面--orderPass");
		OrderProductCustomer opc = new OrderProductCustomer();
		opc.setOrderNo(orderNo);
		List<OrderProductCustomer> queryAll = opcService.queryAll(opc);
		List<OrderPassVo> orderPassVo = new ArrayList<OrderPassVo>();
		if (null != queryAll && queryAll.size() > 0) {
			createOrderFieldList(queryAll, orderPassVo);
			model.put("opc", orderPassVo);
		}
		Order order = new Order();
		order.setOrderNo(orderNo);
		Order queryOne = orderService.queryOne(order);
		if(null!=queryOne){
			model.put("order", queryOne);
		}
		return "/order/orderpass";
	}
	
	
	@ResponseBody
	@RequestMapping("/updateOrderProduct")
	public Map<String,Object> updateOrderProduct(OrderPassFieldVo opVo){
		Map<String,Object> map=new HashMap<String,Object>();
		logger.info("进入产品审核方法");
		OrderProductCustomer opc = new OrderProductCustomer();
		opc.setId(Integer.valueOf(opVo.getId()));
		opc.setProductPrice(Float.valueOf(opVo.getProductPrice()));
		opc.setProductField(opVo.getProductField());
		opc.setProductTime(Float.valueOf(opVo.getProductTime()).intValue());
		opc.setProductType(Float.valueOf(opVo.getProductTime()).intValue());
		opc.setProductStatus(Integer.valueOf(opVo.getProductStatus()));
		opc.setPaymentType(Float.valueOf(opVo.getPaymentType()).intValue());
		opc.setCheckTime(DateUtils.getCurrentDate());
		int i = opcService.updateByPrimaryKeySelective(opc);
		if(i>0){
			map.put("status", true);
		}else{
			map.put("status", false);
		}
		return map;
	}
	
	
	/**
	 * 
	 * 描述：订单审核
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-16 上午10:53:34
	 * @version
	 */
	@ResponseBody
	@RequestMapping("/updateOrder")
	public Map<String,Object> updateOrder(Order order){
		Map<String,Object> map=new HashMap<String,Object>();
		logger.info("进入订单审核方法--updateOrder");
		OrderProductCustomer opc = new OrderProductCustomer();
		opc.setOrderNo(order.getOrderNo());
		opc.setProductStatus(0);
		List<OrderProductCustomer> opcAll = opcService.queryAll(opc);
		if(null!=opcAll && opcAll.size()>0){
			//存在未审核的产品
			map.put("status", false);
			map.put("msg", "当前订单存在未审核的产品,请先审核完所有产品,在进行审核订单操作!");
		}else{
			//所有审核已经通过审核 ( 包含 未通过和已通过)
			order.setOrderStatus(1);
			int i = orderService.updateByPrimaryKeySelective(order);
			if(i>0){
				map.put("status", true);
				map.put("msg", "订单审核完成,按确定后跳转至订单列表页面!");
			}else{
				map.put("status", false);
				map.put("msg", "订单审核失败!");
			}
		}
		return map;
	}

	/**
	 * 
	 * 描述：私有方法，拼装数据
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-15 上午9:53:25
	 * @version
	 */
	private void createOrderFieldList(List<OrderProductCustomer> queryAll, List<OrderPassVo> orderPassVo) {
		for (OrderProductCustomer opc : queryAll) {
			OrderPassVo opVo = new OrderPassVo();
			opVo.setId(opc.getId());
			opVo.setCheckTime(opc.getCheckTime());
			opVo.setCreateTime(opc.getCreateTime());
			opVo.setOrderNo(opc.getOrderNo());
			opVo.setPaymentType(opc.getPaymentType());
			opVo.setProductId(opc.getProductId());
			opVo.setProductName(opc.getProductName());
			opVo.setProductPrice(opc.getProductPrice());
			opVo.setProductStatus(opc.getProductStatus());
			opVo.setProductTime(opc.getProductTime());
			String productField = opc.getProductField();
			//json转为map
			Map<String, Object> map = toMap(productField);
			ProductField pf = new ProductField();
			pf.setProductId(Integer.valueOf(opc.getProductId()));
			pf.setIsDelete(0);
			pf.setIsUse(1);
			//查询 所属产品的属性集合
			List<ProductField> pfquery = pfService.queryAll(pf);
			
			List<OrderFieldVo> orderFieldVoList=new ArrayList<OrderFieldVo>();
			
			//创建field集合
			createOrderFieldVo(map,orderFieldVoList,pfquery);
			
			
			opVo.setOrderFieldList(orderFieldVoList);
			
			orderPassVo.add(opVo);
		}
	}
	
	
	
	/**
	 * 
	 * 描述：私有方法，拼装数据
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-15 上午9:53:48
	 * @version
	 */
	private void createOrderFieldVo(Map<String, Object> map,List<OrderFieldVo> orderFieldVoList,List<ProductField> pfquery){
		for(String key : map.keySet()){
			for (ProductField pfentity : pfquery) {
				if(key.equals(pfentity.getInputName())){
					if("radio".equals(pfentity.getFieldType()) || "select".equals(pfentity.getFieldType())){
						OrderFieldVo ofVo = new OrderFieldVo();
						ofVo.setFieldName(pfentity.getFieldName());
						ofVo.setFieldType(pfentity.getFieldType());
						ofVo.setFieldValue(map.get(key).toString());
						ofVo.setInputName(pfentity.getInputName());
						ProductFieldValue pfv = new ProductFieldValue();
						pfv.setFieldId(pfentity.getId());
						List<ProductFieldValue> queryAll2 = pfvService.queryAll(pfv);
						List<OrderFieldPriceVo> fieldValue=new ArrayList<OrderFieldPriceVo>();
						if(null!=queryAll2 && queryAll2.size()>0){
							for(int i=0;i<queryAll2.size();i++){
								OrderFieldPriceVo fpVo = new OrderFieldPriceVo();
								fpVo.setId(queryAll2.get(i).getId().toString().toString());
								fpVo.setValue(queryAll2.get(i).getFieldValue());
								fieldValue.add(fpVo);
							}
						}
						
						List<OrderFieldPriceVo> fieldPrice=new ArrayList<OrderFieldPriceVo>();
						for (ProductFieldValue pfValue : queryAll2) {
							FieldPrice fp = new FieldPrice();
							fp.setFieldValueId(pfValue.getId());
							fp.setIsDelete(0);
							fp.setIsUse(1);
							FieldPrice queryOne = fpService.queryOne(fp);
							if(null!=queryOne){
								OrderFieldPriceVo fpVo = new OrderFieldPriceVo();
								//Integer price=queryOne.getFieldPrice().intValue();
								fpVo.setId(queryOne.getFieldValueId().toString());
								fpVo.setValue(queryOne.getFieldPrice().toString());
								ofVo.setFieldValue(Float.valueOf(map.get(key).toString()).toString());
								fieldPrice.add(fpVo);
							}
						}
						
						ofVo.setFieldTypeValuePrice(fieldPrice);
						ofVo.setFieldTypeValue(fieldValue);
						orderFieldVoList.add(ofVo);
					}else if("checkbox".equals(pfentity.getFieldType())){
						OrderFieldVo ofVo = new OrderFieldVo();
						ofVo.setFieldName(pfentity.getFieldName());
						ofVo.setFieldType(pfentity.getFieldType());
						ofVo.setFieldValue(map.get(key).toString());
						ofVo.setInputName(pfentity.getInputName());
						ProductFieldValue pfv = new ProductFieldValue();
						pfv.setFieldId(pfentity.getId());
						List<ProductFieldValue> queryAll2 = pfvService.queryAll(pfv);
						List<OrderFieldPriceVo> fieldValue=new ArrayList<OrderFieldPriceVo>();
						if(null!=queryAll2 && queryAll2.size()>0){
							for(int i=0;i<queryAll2.size();i++){
								OrderFieldPriceVo fpVo = new OrderFieldPriceVo();
								fpVo.setId(queryAll2.get(i).getId().toString().toString());
								fpVo.setValue(queryAll2.get(i).getFieldValue());
								fieldValue.add(fpVo);
							}
						}
						
						List<OrderFieldPriceVo> fieldPrice=new ArrayList<OrderFieldPriceVo>();
						for (ProductFieldValue pfValue : queryAll2) {
							FieldPrice fp = new FieldPrice();
							fp.setFieldValueId(pfValue.getId());
							fp.setIsDelete(0);
							fp.setIsUse(1);
							FieldPrice queryOne = fpService.queryOne(fp);
							if(null!=queryOne){
								OrderFieldPriceVo fpVo = new OrderFieldPriceVo();
								//Integer price=queryOne.getFieldPrice().intValue();
								fpVo.setId(queryOne.getFieldValueId().toString());
								fpVo.setValue(queryOne.getFieldPrice().toString());
								//ofVo.setFieldValue(Float.valueOf(map.get(key).toString()).toString());
								fieldPrice.add(fpVo);
							}
						}
						
						ofVo.setFieldTypeValuePrice(fieldPrice);
						ofVo.setFieldTypeValue(fieldValue);
						orderFieldVoList.add(ofVo);
					}else{
						OrderFieldVo ofVo = new OrderFieldVo();
						ofVo.setFieldName(pfentity.getFieldName());
						ofVo.setFieldType(pfentity.getFieldType());
						ofVo.setFieldValue(map.get(key).toString());
						ofVo.setInputName(pfentity.getInputName());
						orderFieldVoList.add(ofVo);
					}
				}
			}
		}
	}

	/**  
	    * 将json格式的字符串解析成Map对象 <li>  
	    * json格式：{"name":"admin","retries":"3fff","testname"  
	    * :"ddd","testretries":"fffffffff"}  
	    */
	private static Map<String, Object> toMap(String json) {
		// JSON -> Map
		//Map<String, Object> map1 = (Map<String, Object>) JSON.parse(json);
		LinkedHashMap<String, Object> map1 = JSON.parseObject(json, new TypeReference<LinkedHashMap<String, Object>>() {
		});
		for (String key : map1.keySet()) {
			/*System.out.println(key + ":" + map1.get(key));*/
		}
		return map1;
	}

}
